﻿using System;
using System.Linq;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Service;
using Icms.WebUI.Builder;
using Icms.WebUI.Dto;
using Omu.Awesome.Mvc;

namespace Icms.WebUI.Controllers
{
    public class TeamController : Cruder<Team, TeamInput>
    {
        private new readonly ICourtsService<Team> s;

        public TeamController(ICourtsService<Team> s, IMapper<Team, TeamInput> v)
            : base(s, v)
        {
            this.s = s;
        }

        public virtual ActionResult Search(string search, int page = 1, int ps = 5)
        {
            var src = s.Where(o => o.Title.StartsWith(search));
            var rows = this.RenderView("rows", src.OrderBy(u => u.Id).Skip((page - 1) * ps).Take(ps));

            return Json(new { rows, more = src.Count() > page * ps });
        }

        public override Guid Creation(Team e)
        {
            return s.Create(e, Globals.Court());
        }
    }
}